//
//  PotentialProjectViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/10/11.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PotentialProjectViewController: RefreshedTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib.init(nibName: "PotentialTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "PotentialTableViewCell")
    }
    
    override func path(for name: String) -> String {
        return PotentialProjectList
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "PotentialTableViewCell")!
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return self.source.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let _model = self.source[indexPath.section]
        var size = CGSize()
        if let _ =  _model.contents?["name"]?.rawString(){
            
            if let customerUnit = _model.contents?["customerUnit"]?.rawString() {
                size = customerUnit.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize(width: UIScreen.main.bounds.size.width - 40, height: 10000))
            }
        } else if let _ = _model.contents?["contractname"]?.rawString() {
            if let customerconpany = _model.contents?["customerconpany"]?.rawString() {
                size = customerconpany.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize(width: UIScreen.main.bounds.size.width - 40, height: 10000))
            }
        } else if let _ = _model.contents?["contractName"]?.rawString() {
            if let customerconpany = _model.contents?["customerConpany"]?.rawString() {
                size = customerconpany.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize(width: UIScreen.main.bounds.size.width - 40, height: 10000))
            }
        }
        return 47 + size.height
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cel = cell as? PotentialTableViewCell {
            cel.model = source[indexPath.section]
        }
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let potential = PotentialDetailViewController()
        potential.title = "潜在项目详情"
        let model = self.source[indexPath.section]
        
        let id = model.contents?["id"]?.rawString() ?? ""
        
        potential.id = id
        
        self.push(destination: potential)
    }

}
